package pers.dawnyang.modular.monitor.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.cache.Cache;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.dawnyang.common.annotation.OperLog;
import pers.dawnyang.common.domain.Result;
import pers.dawnyang.common.enums.OperType;
import pers.dawnyang.common.util.ResultUtil;
import pers.dawnyang.config.cache.CacheUtils;
import pers.dawnyang.modular.monitor.domain.response.cache.CacheKeyRes;
import pers.dawnyang.modular.monitor.domain.response.cache.CacheNameRes;
import pers.dawnyang.modular.monitor.domain.response.cache.CacheValueRes;

/**
 * 缓存监控
 * 
 * @author dawn yang
 *
 */

@Controller
@RequestMapping("/monitor/cache")
public class CacheController {

  @RequiresPermissions("monitor:cache:index")
  @GetMapping("/index")
  public String index() {
    return "/monitor/cache/index";
  }

  @RequiresPermissions("monitor:cache:list")
  @GetMapping("/getCacheNames")
  @ResponseBody
  public Result<List<CacheNameRes>> getCacheNames() {
    List<CacheNameRes> resList = new ArrayList<>();
    int id = 0;
    String[] cacheNames = CacheUtils.getCacheNames();
    for (String str : cacheNames) {
      CacheNameRes res = new CacheNameRes();
      res.setId(++id);
      res.setCacheName(str);
      resList.add(res);
    }
    return ResultUtil.success(resList);
  }

  @RequiresPermissions("monitor:cache:list")
  @GetMapping("/getCacheKeys")
  @ResponseBody
  public Result<List<CacheKeyRes>> getCacheKeys(String cacheName) {
    List<CacheKeyRes> resList = new ArrayList<>();
    int id = 0;
    Cache<String, Object> cacheKeys = CacheUtils.getCache(cacheName);
    for (Object str : cacheKeys.keys()) {
      CacheKeyRes res = new CacheKeyRes();
      res.setId(++id);
      res.setCacheKey(str.toString());
      resList.add(res);
    }
    return ResultUtil.success(resList);
  }

  @RequiresPermissions("monitor:cache:list")
  @GetMapping("/getCacheValue")
  @ResponseBody
  public Result<CacheValueRes> getCacheValue(String cacheName, String cacheKey) {
    Object obj = CacheUtils.get(cacheName, cacheKey);
    String cacheValue = "";
    if (null != obj) {
      cacheValue = obj.toString();
    }
    CacheValueRes res = new CacheValueRes();
    res.setCacheName(cacheName);
    res.setCacheKey(cacheKey);
    res.setCacheValue(cacheValue);
    return ResultUtil.success(res);
  }

  @RequiresPermissions("monitor:cache:del")
  @OperLog(operModul = "缓存管理-清空缓存", operType = OperType.DELETE)
  @PostMapping("/delCacheName")
  @ResponseBody
  public Result<String> delCacheName(String cacheName) {
    CacheUtils.removeAll(cacheName);
    return ResultUtil.success();
  }

  @RequiresPermissions("monitor:cache:del")
  @OperLog(operModul = "缓存管理-删除缓存键值对", operType = OperType.DELETE)
  @PostMapping("/delCacheKey")
  @ResponseBody
  public Result<String> delCacheKey(String cacheName, String cacheKey) {
    CacheUtils.remove(cacheName, cacheKey);
    return ResultUtil.success();
  }

}
